package com.wen.yygh.order.service.impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.wen.yygh.common.utils.RedisUtil;
import com.wen.yygh.enums.PaymentTypeEnum;
import com.wen.yygh.model.order.OrderInfo;
import com.wen.yygh.order.service.OrderInfoService;
import com.wen.yygh.order.service.PaymentService;
import com.wen.yygh.order.service.WeiXinService;
import com.wen.yygh.order.util.ConstantPropertiesUtils;
import com.wen.yygh.order.util.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WeiXinServiceImpl implements WeiXinService {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private RedisUtil redisUtil;

    //生成微信支付二维码
    @Override
    public Map createNative(Long orderId) {
        //根据orderId获取订单信息
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        //向支付记录表中添加信息
        paymentService.savePaymentInfo(orderInfo, PaymentTypeEnum.WEIXIN.getStatus());

        try {
            //从redis获取数据
            Map payMap = (Map) redisUtil.get("weixinPay_" + orderId);
            if (payMap != null) {
                return payMap;
            }
            //设置参数，调用微信生成二维码接口
            //把参数转换xml格式，使用商户key进行加密
            Map paramMap = new HashMap();
            paramMap.put("appid", ConstantPropertiesUtils.APPID);
            paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            String body = orderInfo.getReserveDate() + "就诊" + orderInfo.getDepname();
            paramMap.put("body", body);
            paramMap.put("out_trade_no", orderInfo.getOutTradeNo());
            //paramMap.put("total_fee", order.getAmount().multiply(new BigDecimal("100")).longValue()+"");
            paramMap.put("total_fee", "1");
            paramMap.put("spbill_create_ip", "127.0.0.1");
            paramMap.put("notify_url", "http://guli.shop/api/order/weixinPay/weixinNotify");
            paramMap.put("trade_type", "NATIVE");
            //调用微信生成二维码接口，httpclient调用
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
            client.setHttps(true);
            client.post();
            //返回相关数据
            String xml = client.getContent();
            //转换成map集合
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            log.info("resultMap=>" + resultMap);
            //封装返回结果集
            Map map = new HashMap<>();
            map.put("orderId", orderId);
            map.put("totalFee", orderInfo.getAmount());
            map.put("resultCode", resultMap.get("result_code"));
            map.put("codeUrl", resultMap.get("code_url"));
            if (resultMap.get("result_code") != null) {
                System.out.println("村上了");
                System.out.println(orderId.toString());
                System.out.println(map);
                redisUtil.set("weixinPay_" + orderId, map, 60 * 60 * 2);
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }

    }

    @Override
    public Map<String, String> queryPayStatus(Long orderId) {
        try {

            //1 根据orderId获取订单信息
            OrderInfo orderInfo = orderInfoService.getById(orderId);
            //2 封装提交参数
            Map paramMap = new HashMap();
            paramMap.put("appid", ConstantPropertiesUtils.APPID);
            paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
            paramMap.put("out_trade_no", orderInfo.getOutTradeNo());
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            //3 设置请求内容
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
            client.setHttps(true);
            client.post();
            //4 得到微信接口返回数据
            String xml = client.getContent();
            log.info("xml=>"+xml);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            log.info("resultMap=》" + resultMap);
            //5 吧接口的数据返回
            return resultMap;


        } catch (Exception e) {

        }
        return null;
    }
}
